/**
 * MIT License
 *
 * Copyright (c) 2024 小村程序员
 * Email: 397237738@qq.com
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.cobertura.test.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class PathUtil {
    /**
     * 字节码的根路径
     */
    private static String classes="classes";
    private static String openCoberturaClasses="open-cobertura-classes";

    /**
     * 获取类所在的字节码路径
     * @param className
     * @return
     */
    public static String getClassesPath(String className){
        // 获取Maven项目的根目录
        String baseDir = System.getProperty("user.dir");
        // 获取Maven构建输出目录
        String targetDir = System.getProperty("project.build.directory", "target");
        // 合并根目录和构建输出目录得到完整的target路径
        String targetPath = baseDir + System.getProperty("file.separator") + targetDir;
        String path=targetPath+"/"+classes+className;
        return path;
    }

    public static String getSourcePath(String className){
        // 获取Maven项目的根目录
        String baseDir = System.getProperty("user.dir");
        // 获取Maven构建输出目录
        String source = "src/main/java";
        // 合并根目录和构建输出目录得到完整的target路径
        String sourcePath = baseDir + System.getProperty("file.separator") + source;
        String path=sourcePath+"/"+className;
        return path;
    }

    /**
     * 获取插桩后的字节码路径
     * @param className
     * @return
     */
    public static String getClassesOutPath(String className){
        // 获取Maven项目的根目录
        String baseDir = System.getProperty("user.dir");
        // 获取Maven构建输出目录
        String targetDir = System.getProperty("project.build.directory", "target");
        // 合并根目录和构建输出目录得到完整的target路径
        String targetPath = baseDir + System.getProperty("file.separator") + targetDir;
        String path=targetPath+"/"+openCoberturaClasses+className;
        createDir(path);
        return path;
    }

    public static String getResourcePath(String fileName){
        // 获取Maven项目的根目录
        String baseDir = System.getProperty("user.dir");
        // 获取Maven构建输出目录
        String targetDir = "src/test/resources";
        // 合并根目录和构建输出目录得到完整的target路径
        String targetPath = baseDir +"/"+ targetDir;
        return targetPath+fileName;
    }

    public static void copyClass(String originClass,String targetClassName){
        String originClassPath=getClassesPath(originClass);
        String targetClassPath=getClassesOutPath(targetClassName);
        File originFile=new File(originClassPath);
        File targetFile = new File(targetClassPath, originFile.getName());

        try (FileInputStream fileInputStream = new FileInputStream(originFile);
             FileOutputStream fileOutputStream = new FileOutputStream(targetFile);
             FileChannel sourceChannel = fileInputStream.getChannel();
             FileChannel targetChannel = fileOutputStream.getChannel()) {

            targetChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
            System.out.println("文件复制成功！");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void copyResources(String fileName,String targetClassName){
        String originClassPath=getResourcePath(fileName);
        String targetClassPath=getClassesOutPath(targetClassName);
        File originFile=new File(originClassPath);
        File targetFile = new File(targetClassPath, originFile.getName());

        try (FileInputStream fileInputStream = new FileInputStream(originFile);
             FileOutputStream fileOutputStream = new FileOutputStream(targetFile);
             FileChannel sourceChannel = fileInputStream.getChannel();
             FileChannel targetChannel = fileOutputStream.getChannel()) {

            targetChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
            System.out.println("文件资源复制成功！");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 得到运行数据输出目录
     * @return
     */
    public static String getRuntimeDataOutPath(){
        String baseDir = System.getProperty("user.dir");
        String targetPath = baseDir + "/";
        return targetPath;
    }

    /**
     * 创建目录
     * @param path
     */
    private static void createDir(String path){
        File file=new File(path);
        File fileParent=new File(file.getParent());
        if(!fileParent.exists()){
            fileParent.mkdirs();
        }
    }
}
